#include "desktop.h"
#include "lcd.h"
#include "myhead.h"

#include "jpeglib.h"
#include "api_v4l2.h"

static char g_color_buf[FB_SIZE]={0};
iconlist icon_list;

#define ICON_START 100
#define ICON_STOP  700
#define ICOnum	   10
#define APPnum	   18

iconlist icon_list_init(void)
{
	icon head;
	head.next =NULL;
	head.prev =NULL;
	
	icon *new, *old;	
	old = &head;
	int i;
	for (i = 0; i < ICOnum; ++i)
	{
		new = (icon *)malloc(sizeof(icon));
		sprintf(new->path, "./jpeg/%d.jpg", i+1);
		old->next = new;
		new->prev = old;
		old = new;
	}
	if (new)
	{
		new->next = head.next;
		icon_list = new->next;
	}
	return icon_list;
}

int draw_icon(unsigned int x,unsigned int y,const char *pjpg_path)  
{
	/*定义解码对象，错误处理对象*/
	struct 	jpeg_decompress_struct 	cinfo;
	struct 	jpeg_error_mgr 			jerr;	
	
	char 	*pcolor_buf = g_color_buf;
	char 	*pjpg;
	
	unsigned int 	i=0;
	unsigned int	color =0;
	unsigned int	count =0;
	unsigned int 	x_s = x;
	unsigned int 	x_e ;	
	unsigned int 	y_e ;
			 int	jpg_fd;
	unsigned int 	jpg_size;
	
	unsigned int 	jpg_width;
	unsigned int 	jpg_height;
	/* 申请jpg资源，权限可读可写 */	
	jpg_fd=open(pjpg_path,O_RDWR);	
	if(jpg_fd == -1)
	{
	   printf("open %s error\n",pjpg_path);	   
	   return -1;	
	}		
	jpg_size=file_size_get(pjpg_path);	
	pjpg = malloc(jpg_size);
	read(jpg_fd,pjpg,jpg_size);	
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);
	jpeg_mem_src(&cinfo,pjpg,jpg_size);	
	jpeg_read_header(&cinfo, TRUE);
	jpeg_start_decompress(&cinfo);	

	x_e	= x_s+cinfo.output_width;
	y_e	= y  +cinfo.output_height;	
	while(cinfo.output_scanline < cinfo.output_height )
	{		
		pcolor_buf = g_color_buf;		
		jpeg_read_scanlines(&cinfo,&pcolor_buf,1);		
		for(i=0; i<cinfo.output_width; i++)
		{
			color = 		*(pcolor_buf+2);
			color = color | *(pcolor_buf+1)<<8;
			color = color | *(pcolor_buf)<<16;	
			//越界处理
			if (x > ICON_STOP)
				x= ICON_START;
			lcd_draw_point(x,y,color);			
			pcolor_buf +=3;
			x++;
		}
		y++;			
		x = x_s;		
	}
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	close(jpg_fd);			
	free(pjpg);
	return 0;
}

void dock_refresh(int num)
{
	static unsigned int first = ICON_START;
	//icon
	if (NULL == icon_list)
	{
		if(NULL == icon_list_init())
			printf("icon_list_init error\n");
	}

	int temp;
	temp = first + num;
	if (temp >= ICON_START && temp <= ICON_STOP)
		first = temp;
	if (temp < ICON_START)
		first = ICON_STOP - (ICON_START - temp);
	if (temp > ICON_STOP)
		first = ICON_START + (temp - ICON_STOP);

	int i;
	int x =  first;
	icon *p = icon_list;
	for (i = 0; i < ICOnum; ++i)
	{
		if (x > ICON_STOP)
			x = ICON_START + (x - ICON_STOP);

		draw_icon(x, 420, p->path);
		p = p->next;
		x += 60;
	}
}

void desktop_refresh(int num)
{	
	//背景图
	lcd_draw_jpg(0, 0, BACKGROUND, NULL, 0, 0);
	//top栏
	lcd_draw_jpg(0, 0, TOPBAR, NULL, 0, 0);
	//显示dock栏以及底部icon
	dock_refresh(num);
	//桌面图标
	char path[15];
	int i, j;
	int nump = 0;
	for (i = 0; nump < APPnum; ++i)
	{
		for (j=0; j<6&&nump<APPnum; ++j)
		{
			sprintf(path, "./jpeg/%c.jpg", nump+'a');
			printf("%s\n", path);
			lcd_draw_jpg(20+70*i, 30+60*j, path, NULL, 0, 0);
			nump++;
		}
	}
}

